package au.com.bluelink.scjp.chap6;

import java.util.Scanner;

public class ScannerTest {

	public static void main(String[] args) {

		// the String splitter searches the whole string
		// Scanner provides a better API for scanning a large String
		// and then exiting once we have found a certain pattern (hence the name
		// scanner)
		System.out.println("Testing the String.split(..) method:");
		SplitTest.test();

		// test the Scanner class
		System.out.println("Testing the Scanner API:");
		ScanNext.test();
		
	}

}

class SplitTest {
	public static void test() {

		String sourceString = "ab5 ccc 45 @";
		String delimeterPattern = "\\d";

		String[] tokens = sourceString.split(delimeterPattern);
		System.out.println("count " + tokens.length);
		for (String s : tokens)
			System.out.println(">" + s + "<");
	}
}

class ScanNext {
	public static void test() {
		boolean b2, b;
		int i;
		String s, hits = " ";
		Scanner s1 = new Scanner("1 true 34 hi");
		Scanner s2 = new Scanner("1 true 34 hi");
		while (b = s1.hasNext()) {
			s = s1.next();
			hits += "s";
		}
		while (b = s2.hasNext()) {
			if (s2.hasNextInt()) {
				i = s2.nextInt();
				hits += "i";
			} else if (s2.hasNextBoolean()) {
				b2 = s2.nextBoolean();
				hits += "b";
			} else {
				s2.next();
				hits += "s2";
			}
		}
		System.out.println("hits " + hits);
	}
}